package com.wf.ew.sell.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.sell.biz.OutBillSonBiz;
import com.wf.ew.sell.model.OutBillSon;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-12-06 17:43:46
 * @version ：1.0
 */
@Repository
public class OutBillSonBizImpl extends BaseDao implements OutBillSonBiz {

	@Override
	public String list(PageResult<T> page, OutBillSon outBillSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OutBillSon> rm = new BeanPropertyRowMapper<OutBillSon>(OutBillSon.class);
		StringBuffer sql = new StringBuffer("select o.id,o.outBillId,o.companyId");
        sql.append(",o.warehouseName,o.productName,o.specification,o.lengths");
		sql.append(",o.negative,o.num,o.liWeight,o.actualWeight,o.price,o.totalPrice");
		sql.append(",o.liTotalWeight,o.actualTotalWeight");
		sql.append(",o.model,o.locality,o.createTime,o.createUser");
		sql.append(",o.updateTime,o.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_out_bill_son o ");
        sql.append(" inner join sys_user suser on suser.id=o.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=o.companyId ");
        sql.append(" where o.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and o.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(outBillSon)){
//			if(Utils.isNotEmpty(outBillSon.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", outBillSon.getName());
//			}
			if(Utils.isNotEmpty(outBillSon.getSort())&&Utils.isNotEmpty(outBillSon.getOrder())){
				sql.append(" order by "+outBillSon.getSort()+" "+outBillSon.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OutBillSon> list(OutBillSon outBillSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OutBillSon> rm = new BeanPropertyRowMapper<OutBillSon>(OutBillSon.class);
        StringBuffer sql = new StringBuffer("select o.id,o.outBillId,o.companyId");
        sql.append(",o.warehouseName,o.productName,o.specification,o.lengths");
		sql.append(",o.negative,o.num,o.liWeight,o.actualWeight,o.price,o.totalPrice");
		sql.append(",o.liTotalWeight,o.actualTotalWeight");
		sql.append(",o.model,o.locality,o.createTime,o.createUser");
		sql.append(",o.updateTime,o.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_out_bill_son o ");
        sql.append(" inner join sys_user suser on suser.id=o.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=o.companyId ");
        sql.append(" where o.del=0 and cmy.del=0 and o.outBillId=:outBillId ");
		map.put("outBillId", outBillSon.getOutBillId()+"");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and o.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(outBillSon)){
//			if(Utils.isNotEmpty(outBillSon.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", outBillSon.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OutBillSon getById(OutBillSon outBillSon) {
		RowMapper<OutBillSon> rm = new BeanPropertyRowMapper<OutBillSon>(OutBillSon.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.outBillId,o.companyId");
        sql.append(",o.warehouseName,o.productName,o.specification,o.lengths");
		sql.append(",o.negative,o.num,o.liWeight,o.actualWeight,o.price,o.totalPrice");
		sql.append(",o.liTotalWeight,o.actualTotalWeight");
		sql.append(",o.model,o.locality,o.createTime,o.createUser");
		sql.append(",o.updateTime,o.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_out_bill_son o ");
        sql.append(" inner join sys_user suser on suser.id=o.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=o.companyId ");
        sql.append(" where o.del=0 and cmy.del=0 and o.id=:id ");
		map.put("id", outBillSon.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(OutBillSon outBillSon, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_out_bill_son (");
        sql.append("outBillId,companyId,warehouseName,productName");
		sql.append(",specification,lengths,negative,num,liWeight");
		sql.append(",liTotalWeight,actualTotalWeight");
		sql.append(",actualWeight,price,totalPrice,model,locality");
		sql.append(",createUser) values ( ");
        sql.append(":outBillId,:companyId,:warehouseName,:productName");
		sql.append(",:specification,:lengths,:negative,:num,:liWeight");
		sql.append(",:liTotalWeight,:actualTotalWeight");
		sql.append(",:actualWeight,:price,:totalPrice,:model,:locality");
		sql.append(",:createUser)");
        map.put("outBillId", outBillSon.getOutBillId()==null?null:outBillSon.getOutBillId()+"");
        map.put("companyId", loginUser.getCompanyId());
        map.put("warehouseName", outBillSon.getWarehouseName());
        map.put("productName", outBillSon.getProductName());
        map.put("specification", outBillSon.getSpecification());
        map.put("lengths", outBillSon.getLengths());
        map.put("negative", outBillSon.getNegative());
        map.put("num", outBillSon.getNum()+"");
        map.put("liWeight", outBillSon.getLiWeight());
        map.put("liTotalWeight", outBillSon.getLiTotalWeight());
        map.put("actualTotalWeight", outBillSon.getActualTotalWeight());
        map.put("actualWeight", outBillSon.getActualWeight());
        map.put("price", outBillSon.getPrice());
        map.put("totalPrice", outBillSon.getTotalPrice());
        map.put("model", outBillSon.getModel());
        map.put("locality", outBillSon.getLocality());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OutBillSon outBillSon, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_out_bill_son set ");
        sql.append("outBillId=:outBillId,warehouseName=:warehouseName,productName=:productName ");
		sql.append(",specification=:specification,lengths=:lengths,negative=:negative,num=:num ");
		sql.append(",liTotalWeight=:liTotalWeight,actualTotalWeight=:actualTotalWeight ");
		sql.append(",liWeight=:liWeight,actualWeight=:actualWeight,price=:price ,totalPrice=:totalPrice ");
		sql.append(",model=:model,locality=:locality,updateTime=now() ");
		sql.append(",updateUser=:updateUser where id=:id ");
        map.put("outBillId", outBillSon.getOutBillId()==null?null:outBillSon.getOutBillId()+"");
        map.put("warehouseName", outBillSon.getWarehouseName());
        map.put("productName", outBillSon.getProductName());
        map.put("specification", outBillSon.getSpecification());
        map.put("lengths", outBillSon.getLengths());
        map.put("negative", outBillSon.getNegative());
        map.put("num", outBillSon.getNum()+"");
        map.put("liTotalWeight", outBillSon.getLiTotalWeight());
        map.put("actualTotalWeight", outBillSon.getActualTotalWeight());
        map.put("liWeight", outBillSon.getLiWeight());
        map.put("actualWeight", outBillSon.getActualWeight());
        map.put("price", outBillSon.getPrice());
        map.put("totalPrice", outBillSon.getTotalPrice());
        map.put("model", outBillSon.getModel());
        map.put("locality", outBillSon.getLocality());
		map.put("updateUser", loginUser.getId());
		map.put("id", outBillSon.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(OutBillSon outBillSon, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(outBillSon.getIds().length()>0){
			for(String ID : outBillSon.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_out_bill_son set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
